package com.zkzx.operation.api.service.authorization;

import com.zkzx.operation.api.mapper.UserRoleMapper;
import com.zkzx.operation.api.mapper.entity.UserRole;
import com.zkzx.operation.api.mapper.entity.UserRoleExample;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月27日 17时23分
 */
@Service
public class UserRoleService {

    @Resource
    UserRoleMapper userRoleMapper;

    /**
     * 获取用户对应的角色信息
     * @param userId 用户id
     * @return 角色信息
     */
    public UserRole getByUserId(Integer userId){
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUseridEqualTo(userId);
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
        Optional<UserRole> userRoleFirst = userRoles.stream().findFirst();
        if(!userRoleFirst.isPresent()){
            throw new AccessDeniedException("没有权限");
        }
        // 获取用户管理的角色
        return userRoleFirst.get();
    }
}
